/**
 * Choicesoft.com.cn Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package com.chen.ssh;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.wm.WindowManager;
import com.jcraft.jsch.Session;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author chenxubing
 * @version $Id: SshFrame.java, v 0.1 2018-12-05 11:17 AM chenxubing Exp $$
 */
public class SshFrame extends JFrame {
    private static final int FRAME_WIDTH = 520;
    private static final int FRAME_HEIGHT = 450;
    private static final int PANAL_MAIN_WIDTH = 518;
    private static final int PANAL_MAIN_HEIGHT = 380;

    private static final String SSH_HOST = "";
    private static final String SSH_PORT = "";
    private static final String SSH_USER = "";
    private static final String SSH_PASSWORD = "";
    private static final String HOST = "";
    private static final String PORT = "";
    private static final String USER = "";
    private static final String PASSWORD = "";
    private static final String LOCAL_PORT = "";

    private Project project;
    private Session session;
    private boolean connectOk;

    public SshFrame(Project project) {
        this.project = project;
        init();
    }

    private void init() {
        FlowLayout flowLayout = new FlowLayout();
        this.setLayout(flowLayout);

        JPanel panalMain = new JPanel();
        GridLayout gridLayOut = new GridLayout();
        gridLayOut.setColumns(2);
        gridLayOut.setRows(10);
        gridLayOut.setVgap(3);
        gridLayOut.setHgap(2);
        panalMain.setLayout(gridLayOut);
        panalMain.setSize(SshFrame.PANAL_MAIN_WIDTH, SshFrame.PANAL_MAIN_HEIGHT);

        Font font = new Font("宋体", 1, 12);
        JLabel labSshHost = new JLabel("SSH HOST");
        labSshHost.setFont(font);
        JLabel labSshPort = new JLabel("SSH PORT");
        labSshPort.setFont(font);
        JLabel labSshUser = new JLabel("SSH USER");
        labSshUser.setFont(font);
        JLabel labSshPassword = new JLabel("SSH PASSWORD");
        labSshPassword.setFont(font);

        JLabel labHost = new JLabel("HOST");
        labHost.setFont(font);
        JLabel labPort = new JLabel("PORT");
        labPort.setFont(font);
        JLabel labUser = new JLabel("USER");
        labUser.setFont(font);
        JLabel labPassword = new JLabel("PASSWORD");
        labPassword.setFont(font);

        JLabel labLocalPort = new JLabel("LOCAL PORT");
        labLocalPort.setFont(font);

        JTextField fieldSshHost = new JTextField(SSH_HOST);
        fieldSshHost.setFont(font);
        fieldSshHost.setColumns(20);
        JTextField fieldSshPort = new JTextField(SSH_PORT);
        fieldSshPort.setFont(font);
        JTextField fieldSshUser = new JTextField(SSH_USER);
        fieldSshUser.setFont(font);
        JTextField fieldSshPassword = new JTextField(SSH_PASSWORD);
        fieldSshPassword.setFont(font);
        JTextField fieldHost = new JTextField(HOST);
        fieldHost.setFont(font);
        JTextField fieldPort = new JTextField(PORT);
        fieldPort.setFont(font);
        JTextField fieldUser = new JTextField(USER);
        fieldUser.setFont(font);
        JTextField fieldPassword = new JTextField(PASSWORD);
        fieldPassword.setFont(font);
        JTextField fieldLocalPort = new JTextField(LOCAL_PORT);
        fieldLocalPort.setFont(font);

        JTextArea areaRes = new JTextArea();
        areaRes.setColumns(42);
        areaRes.setRows(5);
        areaRes.setFont(font);

        JButton btnDo = new JButton("连接");
        btnDo.setFont(font);
        btnDo.addActionListener((e) -> {
            if (connectOk) {
                return;
            }
            Color color = btnDo.getBackground();
            try {
                String sshHost = fieldSshHost.getText().trim();
                int sshPort = NumberUtils.toInt(fieldSshPort.getText());
                String sshUser = fieldSshUser.getText().trim();
                String sshPassword = fieldSshPassword.getText();
                String host = fieldHost.getText().trim();
                int port = NumberUtils.toInt(fieldPort.getText());
                int localPort = NumberUtils.toInt(fieldLocalPort.getText());
                session = SshUtils.openSession(sshHost, sshPort, sshUser, sshPassword, host, port, localPort);
                String user = fieldUser.getText().trim();
                areaRes.setText("ssh " + user + "@localhost -p " + localPort);
                btnDo.setBackground(Color.RED);
                connectOk = true;
            } catch (Exception e2) {
                connectOk = false;
                btnDo.setBackground(color);
                Messages.showMessageDialog(SshFrame.this, "出错了", "出错了", Messages.getInformationIcon());
                areaRes.setText(e2.getMessage());
            }
        });

        JButton btnClear = new JButton("断开");
        btnClear.setFont(font);
        btnClear.addActionListener((e) -> {
            connectOk = false;
            Color color = btnClear.getBackground();
            SshUtils.closeSession(session);
            btnDo.setBackground(color);
        });

        panalMain.add(labSshHost);
        panalMain.add(fieldSshHost);
        panalMain.add(labSshPort);
        panalMain.add(fieldSshPort);
        panalMain.add(labSshUser);
        panalMain.add(fieldSshUser);
        panalMain.add(labSshPassword);
        panalMain.add(fieldSshPassword);

        panalMain.add(labHost);
        panalMain.add(fieldHost);
        panalMain.add(labPort);
        panalMain.add(fieldPort);
        panalMain.add(labUser);
        panalMain.add(fieldUser);
        panalMain.add(labPassword);
        panalMain.add(fieldPassword);
        panalMain.add(labLocalPort);
        panalMain.add(fieldLocalPort);
        panalMain.add(btnDo);
        panalMain.add(btnClear);

        panalMain.setVisible(true);
        add(panalMain);

        JPanel panelRes = new JPanel();
        panelRes.add(areaRes);
        add(panelRes);

        this.setTitle("SSH隧道");
        this.setSize(SshFrame.FRAME_WIDTH, SshFrame.FRAME_HEIGHT);
        this.setResizable(false);
        this.setVisible(true);
        Window window = WindowManager.getInstance().suggestParentWindow(project);
        this.setLocationRelativeTo(window);
    }
}
